home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / non-ANSI / c-client / os_bsd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-28  |  20.1 KB  |  757 lines

  1. /*
  2.  * Program:    Operating-system dependent routines -- 4.3BSD version
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *
  11.  * Date:    11 May 1989
  12.  * Last Edited:    28 July 1993
  13.  *
  14.  * Copyright 1993 by the University of Washington
  15.  *
  16.  *  Permission to use, copy, modify, and distribute this software and its
  17.  * documentation for any purpose and without fee is hereby granted, provided
  18.  * that the above copyright notice appears in all copies and that both the
  19.  * above copyright notice and this permission notice appear in supporting
  20.  * documentation, and that the name of the University of Washington not be
  21.  * used in advertising or publicity pertaining to distribution of the software
  22.  * without specific, written prior permission.  This software is made
  23.  * available "as is", and
  24.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  25.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  26.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  27.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  28.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  29.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  30.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  31.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32.  *
  33.  */
  34.  
  35. #define isodigit(c)    (((unsigned)(c)>=060)&((unsigned)(c)<=067))
  36. #define toint(c)       ((c)-'0')
  37.  
  38.  
  39. /* TCP input buffer */
  40.  
  41. #define BUFLEN 8192
  42.  
  43.  
  44. /* TCP I/O stream (must be before osdep.h is included) */
  45.  
  46. #define TCPSTREAM struct tcp_stream
  47. TCPSTREAM {
  48.   char *host;            /* host name */
  49.   char *localhost;        /* local host name */
  50.   int tcpsi;            /* input socket */
  51.   int tcpso;            /* output socket */
  52.   int ictr;            /* input counter */
  53.   char *iptr;            /* input pointer */
  54.   char ibuf[BUFLEN];        /* input buffer */
  55. };
  56.  
  57.  
  58. #include "osdep.h"
  59. #include <sys/time.h>
  60. #include <sys/socket.h>
  61. #include <netinet/in.h>
  62. #include <netdb.h>
  63. #include <ctype.h>
  64. #include <errno.h>
  65. extern int errno;        /* just in case */
  66. #include <pwd.h>
  67. #include <syslog.h>
  68. #include "mail.h"
  69. #include "misc.h"
  70.  
  71. /* Write current time in RFC 822 format
  72.  * Accepts: destination string
  73.  */
  74.  
  75. char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  76.  
  77. void rfc822_date (date)
  78.     char *date;
  79. {
  80.   int zone;
  81.   char *zonename;
  82.   struct tm *t;
  83.   struct timeval tv;
  84.   struct timezone tz;
  85.   gettimeofday (&tv,&tz);    /* get time and timezone poop */
  86.   t = localtime (&tv.tv_sec);    /* convert to individual items */
  87.   zone = t->tm_gmtoff/60;    /* get timezone from TZ environment stuff */
  88.   zonename = t->tm_zone;
  89.                 /* and output it */
  90.   sprintf (date,"%s, %d %s %d %02d:%02d:%02d %+03d%02d (%s)",
  91.        days[t->tm_wday],t->tm_mday,months[t->tm_mon],t->tm_year+1900,
  92.        t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60,zonename);
  93. }
  94.  
  95. /* Get a block of free storage
  96.  * Accepts: size of desired block
  97.  * Returns: free storage block
  98.  */
  99.  
  100. void *fs_get (size)
  101.     size_t size;
  102. {
  103.   void *block = malloc (size);
  104.   if (!block) fatal ("Out of free storage");
  105.   return (block);
  106. }
  107.  
  108.  
  109. /* Resize a block of free storage
  110.  * Accepts: ** pointer to current block
  111.  *        new size
  112.  */
  113.  
  114. void fs_resize (block,size)
  115.     void **block;
  116.     size_t size;
  117. {
  118.   if (!(*block = realloc (*block,size))) fatal ("Can't resize free storage");
  119. }
  120.  
  121.  
  122. /* Return a block of free storage
  123.  * Accepts: ** pointer to free storage block
  124.  */
  125.  
  126. void fs_give (block)
  127.     void **block;
  128. {
  129.   free (*block);
  130.   *block = NIL;
  131. }
  132.  
  133.  
  134. /* Report a fatal error
  135.  * Accepts: string to output
  136.  */
  137.  
  138. void fatal (string)
  139.     char *string;
  140. {
  141.   mm_fatal (string);        /* output the string */
  142.   syslog (LOG_ALERT,"IMAP C-Client crash: %s",string);
  143.   abort ();            /* die horribly */
  144. }
  145.  
  146. /* Copy string with CRLF newlines
  147.  * Accepts: destination string
  148.  *        pointer to size of destination string
  149.  *        source string
  150.  *        length of source string
  151.  */
  152.  
  153. char *strcrlfcpy (dst,dstl,src,srcl)
  154.     char **dst;
  155.     unsigned long *dstl;
  156.     char *src;
  157.     unsigned long srcl;
  158. {
  159.   long i,j;
  160.   char *d = src;
  161.                 /* count number of LF's in source string(s) */
  162.   for (i = srcl,j = 0; j < srcl; j++) if (*d++ == '\012') i++;
  163.   if (i > *dstl) {        /* resize if not enough space */
  164.     fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  165.     *dst = (char *) fs_get ((*dstl = i) + 1);
  166.   }
  167.   d = *dst;            /* destination string */
  168.                 /* copy strings, inserting CR's before LF's */
  169.   while (srcl--) switch (*src) {
  170.   case '\015':            /* unlikely carriage return */
  171.     *d++ = *src++;        /* copy it and any succeeding linefeed */
  172.     if (srcl && *src == '\012') {
  173.       *d++ = *src++;
  174.       srcl--;
  175.     }
  176.     break;
  177.   case '\012':            /* line feed? */
  178.     *d++ ='\015';        /* yes, prepend a CR, drop into default case */
  179.   default:            /* ordinary chararacter */
  180.     *d++ = *src++;        /* just copy character */
  181.     break;
  182.   }
  183.   *d = '\0';            /* tie off destination */
  184.   return *dst;            /* return destination */
  185. }
  186.  
  187.  
  188. /* Length of string after strcrlfcpy applied
  189.  * Accepts: source string
  190.  *        length of source string
  191.  */
  192.  
  193. unsigned long strcrlflen (s)
  194.     STRING *s;
  195. {
  196.   unsigned long pos = GETPOS (s);
  197.   unsigned long i = SIZE (s);
  198.   unsigned long j = i;
  199.   while (j--) switch (SNX (s)) {/* search for newlines */
  200.   case '\015':            /* unlikely carriage return */
  201.     if (j && (CHR (s) == '\012')) {
  202.       SNX (s);            /* eat the line feed */
  203.       j--;
  204.     }
  205.     break;
  206.   case '\012':            /* line feed? */
  207.     i++;
  208.   default:            /* ordinary chararacter */
  209.     break;
  210.   }
  211.   SETPOS (s,pos);        /* restore old position */
  212.   return i;
  213. }
  214.  
  215. /* Server log in
  216.  * Accepts: user name string
  217.  *        password string
  218.  *        optional place to return home directory
  219.  * Returns: T if password validated, NIL otherwise
  220.  */
  221.  
  222. long server_login (user,pass,home,argc,argv)
  223.     char *user;
  224.     char *pass;
  225.     char **home;
  226.     int argc;
  227.     char *argv[];
  228. {
  229.   struct passwd *pw = getpwnam (lcase (user));
  230.                 /* no entry for this user or root */
  231.   if (!(pw && pw->pw_uid)) return NIL;
  232.                 /* validate password */
  233.   if (strcmp (pw->pw_passwd,(char *) crypt (pass,pw->pw_passwd))) return NIL;
  234.   setgid (pw->pw_gid);        /* all OK, login in as that user */
  235.   initgroups (user,pw->pw_gid);    /* initialize groups */
  236.   setuid (pw->pw_uid);
  237.                 /* note home directory */
  238.   if (home) *home = cpystr (pw->pw_dir);
  239.   return T;
  240. }
  241.  
  242. /* Return my user name
  243.  * Returns: my user name
  244.  */
  245.  
  246. char *uname = NIL;
  247.  
  248. char *myusername ()
  249. {
  250.   return uname ? uname : (uname = cpystr (getpwuid (geteuid ())->pw_name));
  251. }
  252.  
  253.  
  254. /* Return my home directory name
  255.  * Returns: my home directory name
  256.  */
  257.  
  258. char *hdname = NIL;
  259.  
  260. char *myhomedir ()
  261. {
  262.   return hdname ? hdname : (hdname = cpystr (getpwuid (geteuid ())->pw_dir));
  263. }
  264.  
  265.  
  266. /* Build status lock file name
  267.  * Accepts: scratch buffer
  268.  *        file name
  269.  * Returns: name of file to lock
  270.  */
  271.  
  272. char *lockname (tmp,fname)
  273.     char *tmp;
  274.     char *fname;
  275. {
  276.   int i;
  277.   sprintf (tmp,"/tmp/.%s",fname);
  278.   for (i = 6; i < strlen (tmp); ++i) if (tmp[i] == '/') tmp[i] = '\\';
  279.   return tmp;            /* note name for later */
  280. }
  281.  
  282. /* TCP/IP open
  283.  * Accepts: host name
  284.  *        contact port number
  285.  * Returns: TCP/IP stream if success else NIL
  286.  */
  287.  
  288. TCPSTREAM *tcp_open (host,port)
  289.     char *host;
  290.     long port;
  291. {
  292.   TCPSTREAM *stream = NIL;
  293.   int sock;
  294.   char *s;
  295.   struct sockaddr_in sin;
  296.   struct hostent *host_name;
  297.   char hostname[MAILTMPLEN];
  298.   char tmp[MAILTMPLEN];
  299.   /* The domain literal form is used (rather than simply the dotted decimal
  300.      as with other Unix programs) because it has to be a valid "host name"
  301.      in mailsystem terminology. */
  302.                 /* look like domain literal? */
  303.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  304.     strcpy (hostname,host+1);    /* yes, copy number part */
  305.     hostname[(strlen (hostname))-1] = '\0';
  306.     if ((sin.sin_addr.s_addr = inet_addr (hostname)) != -1) {
  307.       sin.sin_family = AF_INET;    /* family is always Internet */
  308.       strcpy (hostname,host);    /* hostname is user's argument */
  309.     }
  310.     else {
  311.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  312.       mm_log (tmp,ERROR);
  313.       return NIL;
  314.     }
  315.   }
  316.  
  317.   else {            /* lookup host name, note that brain-dead Unix
  318.                    requires lowercase! */
  319.     strcpy (hostname,host);    /* in case host is in write-protected memory */
  320.     if ((host_name = gethostbyname (lcase (hostname)))) {
  321.                 /* copy address type */
  322.       sin.sin_family = host_name->h_addrtype;
  323.                 /* copy host name */
  324.       strcpy (hostname,host_name->h_name);
  325.                 /* copy host addresses */
  326.       memcpy (&sin.sin_addr,host_name->h_addr,host_name->h_length);
  327.     }
  328.     else {
  329.       sprintf (tmp,"No such host as %.80s",host);
  330.       mm_log (tmp,ERROR);
  331.       return NIL;
  332.     }
  333.   }
  334.  
  335.                 /* copy port number in network format */
  336.   if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
  337.                 /* get a TCP stream */
  338.   if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) {
  339.     sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno));
  340.     mm_log (tmp,ERROR);
  341.     return NIL;
  342.   }
  343.                 /* open connection */
  344.   if (connect (sock,(struct sockaddr *)&sin,sizeof (sin)) < 0) {
  345.     sprintf (tmp,"Can't connect to %.80s,%d: %s",hostname,port,
  346.          strerror (errno));
  347.     mm_log (tmp,ERROR);
  348.     return NIL;
  349.   }
  350.                 /* create TCP/IP stream */
  351.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  352.                 /* copy official host name */
  353.   stream->host = cpystr (hostname);
  354.                 /* get local name */
  355.   gethostname (tmp,MAILTMPLEN-1);
  356.   stream->localhost = cpystr ((host_name = gethostbyname (tmp)) ?
  357.                   host_name->h_name : tmp);
  358.                 /* init sockets */
  359.   stream->tcpsi = stream->tcpso = sock;
  360.   stream->ictr = 0;        /* init input counter */
  361.   return stream;        /* return success */
  362. }
  363.  
  364. /* TCP/IP authenticated open
  365.  * Accepts: host name
  366.  *        service name
  367.  * Returns: TCP/IP stream if success else NIL
  368.  */
  369.  
  370. TCPSTREAM *tcp_aopen (host,service)
  371.     char *host;
  372.     char *service;
  373. {
  374.   TCPSTREAM *stream = NIL;
  375.   struct hostent *host_name;
  376.   char hostname[MAILTMPLEN];
  377.   int i;
  378.   int pipei[2],pipeo[2];
  379.   /* The domain literal form is used (rather than simply the dotted decimal
  380.      as with other Unix programs) because it has to be a valid "host name"
  381.      in mailsystem terminology. */
  382.                 /* look like domain literal? */
  383.   if (host[0] == '[' && host[i = (strlen (host))-1] == ']') {
  384.     strcpy (hostname,host+1);    /* yes, copy without brackets */
  385.     hostname[i-1] = '\0';
  386.   }
  387.                 /* note that Unix requires lowercase! */
  388.   else if (host_name = gethostbyname (lcase (strcpy (hostname,host))))
  389.     strcpy (hostname,host_name->h_name);
  390.                 /* make command pipes */
  391.   if (pipe (pipei) < 0) return NIL;
  392.   if (pipe (pipeo) < 0) {
  393.     close (pipei[0]); close (pipei[1]);
  394.     return NIL;
  395.   }
  396.   if ((i = fork ()) < 0) {    /* make inferior process */
  397.     close (pipei[0]); close (pipei[1]);
  398.     close (pipeo[0]); close (pipeo[1]);
  399.     return NIL;
  400.   }
  401.   if (i) {            /* parent? */
  402.     close (pipei[1]);        /* close child's side of the pipes */
  403.     close (pipeo[0]);
  404.   }
  405.   else {            /* child */
  406.     dup2 (pipei[1],1);        /* parent's input is my output */
  407.     dup2 (pipei[1],2);        /* parent's input is my error output too */
  408.     close (pipei[0]); close (pipei[1]);
  409.     dup2 (pipeo[0],0);        /* parent's output is my input */
  410.     close (pipeo[0]); close (pipeo[1]);
  411.                 /* now run it */
  412.     execl ("/usr/ucb/rsh","rsh",hostname,"exec",service,0);
  413.     _exit (1);            /* spazzed */
  414.   }
  415.  
  416.                 /* create TCP/IP stream */
  417.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  418.                 /* copy official host name */
  419.   stream->host = cpystr (hostname);
  420.                 /* get local name */
  421.   gethostname (hostname,MAILTMPLEN-1);
  422.   stream->localhost = cpystr ((host_name = gethostbyname (hostname)) ?
  423.                   host_name->h_name : hostname);
  424.   stream->tcpsi = pipei[0];    /* init sockets */
  425.   stream->tcpso = pipeo[1];
  426.   stream->ictr = 0;        /* init input counter */
  427.   return stream;        /* return success */
  428. }
  429.  
  430. /* TCP/IP receive line
  431.  * Accepts: TCP/IP stream
  432.  * Returns: text line string or NIL if failure
  433.  */
  434.  
  435. char *tcp_getline (stream)
  436.     TCPSTREAM *stream;
  437. {
  438.   int n,m;
  439.   char *st,*ret,*stp;
  440.   char c = '\0';
  441.   char d;
  442.                 /* make sure have data */
  443.   if (!tcp_getdata (stream)) return NIL;
  444.   st = stream->iptr;        /* save start of string */
  445.   n = 0;            /* init string count */
  446.   while (stream->ictr--) {    /* look for end of line */
  447.     d = *stream->iptr++;    /* slurp another character */
  448.     if ((c == '\015') && (d == '\012')) {
  449.       ret = (char *) fs_get (n--);
  450.       memcpy (ret,st,n);    /* copy into a free storage string */
  451.       ret[n] = '\0';        /* tie off string with null */
  452.       return ret;
  453.     }
  454.     n++;            /* count another character searched */
  455.     c = d;            /* remember previous character */
  456.   }
  457.                 /* copy partial string from buffer */
  458.   memcpy ((ret = stp = (char *) fs_get (n)),st,n);
  459.                 /* get more data from the net */
  460.   if (!tcp_getdata (stream)) return NIL;
  461.                 /* special case of newline broken by buffer */
  462.   if ((c == '\015') && (*stream->iptr == '\012')) {
  463.     stream->iptr++;        /* eat the line feed */
  464.     stream->ictr--;
  465.     ret[n - 1] = '\0';        /* tie off string with null */
  466.   }
  467.                 /* else recurse to get remainder */
  468.   else if (st = tcp_getline (stream)) {
  469.     ret = (char *) fs_get (n + 1 + (m = strlen (st)));
  470.     memcpy (ret,stp,n);        /* copy first part */
  471.     memcpy (ret + n,st,m);    /* and second part */
  472.     fs_give ((void **) &stp);    /* flush first part */
  473.     fs_give ((void **) &st);    /* flush second part */
  474.     ret[n + m] = '\0';        /* tie off string with null */
  475.   }
  476.   return ret;
  477. }
  478.  
  479. /* TCP/IP receive buffer
  480.  * Accepts: TCP/IP stream
  481.  *        size in bytes
  482.  *        buffer to read into
  483.  * Returns: T if success, NIL otherwise
  484.  */
  485.  
  486. long tcp_getbuffer (stream,size,buffer)
  487.     TCPSTREAM *stream;
  488.     unsigned long size;
  489.     char *buffer;
  490. {
  491.   unsigned long n;
  492.   char *bufptr = buffer;
  493.   while (size > 0) {        /* until request satisfied */
  494.     if (!tcp_getdata (stream)) return NIL;
  495.     n = min (size,stream->ictr);/* number of bytes to transfer */
  496.                 /* do the copy */
  497.     memcpy (bufptr,stream->iptr,n);
  498.     bufptr += n;        /* update pointer */
  499.     stream->iptr +=n;
  500.     size -= n;            /* update # of bytes to do */
  501.     stream->ictr -=n;
  502.   }
  503.   bufptr[0] = '\0';        /* tie off string */
  504.   return T;
  505. }
  506.  
  507.  
  508. /* TCP/IP receive data
  509.  * Accepts: TCP/IP stream
  510.  * Returns: T if success, NIL otherwise
  511.  */
  512.  
  513. long tcp_getdata (stream)
  514.     TCPSTREAM *stream;
  515. {
  516.   fd_set fds;
  517.   FD_ZERO (&fds);        /* initialize selection vector */
  518.   if (stream->tcpsi < 0) return NIL;
  519.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  520.     FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */
  521.                 /* block and read */
  522.     if ((select (stream->tcpsi+1,&fds,0,0,0) < 0) ||
  523.     ((stream->ictr = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 1)) {
  524.       close (stream->tcpsi);    /* nuke the socket */
  525.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  526.       stream->tcpsi = stream->tcpso = -1;
  527.       return NIL;
  528.     }
  529.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  530.   }
  531.   return T;
  532. }
  533.  
  534. /* TCP/IP send string as record
  535.  * Accepts: TCP/IP stream
  536.  *        string pointer
  537.  * Returns: T if success else NIL
  538.  */
  539.  
  540. long tcp_soutr (stream,string)
  541.     TCPSTREAM *stream;
  542.     char *string;
  543. {
  544.   return tcp_sout (stream,string,(unsigned long) strlen (string));
  545. }
  546.  
  547.  
  548. /* TCP/IP send string
  549.  * Accepts: TCP/IP stream
  550.  *        string pointer
  551.  *        byte count
  552.  * Returns: T if success else NIL
  553.  */
  554.  
  555. long tcp_sout (stream,string,size)
  556.     TCPSTREAM *stream;
  557.     char *string;
  558.     unsigned long size;
  559. {
  560.   int i;
  561.   fd_set fds;
  562.   FD_ZERO (&fds);        /* initialize selection vector */
  563.   if (stream->tcpso < 0) return NIL;
  564.   while (size > 0) {        /* until request satisfied */
  565.     FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
  566.     if ((select (stream->tcpso+1,0,&fds,0,0) < 0) ||
  567.     ((i = write (stream->tcpso,string,size)) < 0)) {
  568.       puts (strerror (errno));
  569.       close (stream->tcpsi);    /* nuke the socket */
  570.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  571.       stream->tcpsi = stream->tcpso = -1;
  572.       return NIL;
  573.     }
  574.     size -= i;            /* count this size */
  575.     string += i;
  576.   }
  577.   return T;            /* all done */
  578. }
  579.  
  580. /* TCP/IP close
  581.  * Accepts: TCP/IP stream
  582.  */
  583.  
  584. void tcp_close (stream)
  585.     TCPSTREAM *stream;
  586. {
  587.  
  588.   if (stream->tcpsi >= 0) {    /* no-op if no socket */
  589.     close (stream->tcpsi);    /* nuke the socket */
  590.     if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  591.     stream->tcpsi = stream->tcpso = -1;
  592.   }
  593.                 /* flush host names */
  594.   fs_give ((void **) &stream->host);
  595.   fs_give ((void **) &stream->localhost);
  596.   fs_give ((void **) &stream);    /* flush the stream */
  597. }
  598.  
  599.  
  600. /* TCP/IP get host name
  601.  * Accepts: TCP/IP stream
  602.  * Returns: host name for this stream
  603.  */
  604.  
  605. char *tcp_host (stream)
  606.     TCPSTREAM *stream;
  607. {
  608.   return stream->host;        /* return host name */
  609. }
  610.  
  611.  
  612. /* TCP/IP get local host name
  613.  * Accepts: TCP/IP stream
  614.  * Returns: local host name
  615.  */
  616.  
  617. char *tcp_localhost (stream)
  618.     TCPSTREAM *stream;
  619. {
  620.   return stream->localhost;    /* return local host name */
  621. }
  622.  
  623.  
  624.  
  625. /* Return pointer to first occurance in string of a substring
  626.  * Accepts: source pointer
  627.  *        substring pointer
  628.  * Returns: pointer to substring in source or NIL if not found
  629.  */
  630.  
  631. char *strstr (cs,ct)
  632.      char *cs;
  633.      char *ct;
  634. {
  635.   char *s;
  636.   char *t;
  637.   while (cs = strchr (cs,*ct)) {/* for each occurance of the first character */
  638.                 /* see if remainder of string matches */
  639.     for (s = cs + 1, t = ct + 1; *t && *s == *t; s++, t++);
  640.     if (!*t) return cs;        /* if ran out of substring then have match */
  641.     cs++;            /* try from next character */
  642.   }
  643.   return NIL;            /* not found */
  644. }
  645.  
  646. /* Return implementation-defined string corresponding to error
  647.  * Accepts: error number
  648.  * Returns: string for that error
  649.  */
  650.  
  651. char *strerror (n)
  652.      int n;
  653. {
  654.   extern char *sys_errlist[];
  655.   extern int sys_nerr;
  656.  
  657.   return (n >= 0 && n < sys_nerr) ? sys_errlist[n] : NIL;
  658. }
  659.  
  660.  
  661.  
  662. /* Copy memory block
  663.  * Accepts: destination pointer
  664.  *        source pointer
  665.  *        length
  666.  * Returns: destination pointer
  667.  */
  668.  
  669. char *memmove (s,ct,n)
  670.      char *s;
  671.      char *ct;
  672.      int n;
  673. {
  674.   bcopy (ct,s,n);        /* they should have this one */
  675.   return ct;
  676. }
  677.  
  678.  
  679. /*
  680.  * Turn a string long into the real thing
  681.  * Accepts: source string
  682.  *        pointer to place to return end pointer
  683.  *        base
  684.  * Returns: parsed long integer, end pointer is updated
  685.  */
  686.  
  687. long strtol (s,endp,base)
  688.      char *s;
  689.      char **endp;
  690.      int base;
  691. {
  692.   long value = 0;        /* the accumulated value */
  693.   int negative = 0;        /* this a negative number? */
  694.   int ok;            /* true while valid chars for number */
  695.   char c;
  696.                 /* insist upon valid base */
  697.   if (base && (base < 2 || base > 36)) return NIL;
  698.   while (isspace (*s)) s++;    /* skip leading whitespace */
  699.   if (!base) {            /* if base = 0, */
  700.     if (*s == '-') {        /* integer constants are allowed a */
  701.       negative = 1;        /* leading unary minus, but not */
  702.       s++;            /* unary plus. */
  703.     }
  704.     else negative = 0;
  705.     if (*s == '0') {        /* if it starts with 0, its either */
  706.                 /* 0X, which marks a hex constant, */
  707.       if (toupper (*++s) == 'X') {
  708.     s++;            /* skip the X */
  709.            base = 16;        /* base is hex 16 */
  710.       }
  711.       else base = 8;        /* leading 0 means octal number */
  712.     }
  713.     else base = 10;        /* otherwise, decimal. */
  714.     do {
  715.       switch (base) {        /* char has to be valid for base */
  716.       case 8:            /* this digit ok? */
  717.     ok = isodigit(*s);
  718.     break;
  719.       case 10:
  720.     ok = isdigit(*s);
  721.     break;
  722.       case 16:
  723.     ok = isxdigit(*s);
  724.     break;
  725.       default:            /* it's good form you know */
  726.     return NIL;
  727.       }
  728.                 /* if valid char, accumulate */
  729.       if (ok) value = value * base + toint(*s++);
  730.     } while (ok);
  731.     if (toupper(*s) == 'L') s++;/* ignore 'l' or 'L' marker */
  732.     if (endp) *endp = s;    /* set user pointer to after num */
  733.     return (negative) ? -value : value;
  734.   }
  735.  
  736.   switch (*s) {            /* check for leading sign char */
  737.   case '-':
  738.     negative = 1;        /* yes, negative #.  fall into '+' */
  739.   case '+':
  740.     s++;            /* skip the sign character */
  741.   }
  742.                 /* allow hex prefix "0x" */
  743.   if (base == 16 && *s == '0' && toupper (*(s + 1)) == 'X') s += 2;
  744.   do {
  745.                 /* convert to numeric form if digit*/
  746.     if (isdigit (*s)) c = toint (*s);
  747.                 /* alphabetic conversion */
  748.     else if (isalpha (*s)) c = *s - (isupper (*s) ? 'A' : 'a') + 10;
  749.     else break;            /* else no way it's valid */
  750.     if (c >= base) break;    /* digit out of range for base? */
  751.     value = value * base + c;    /* accumulate the digit */
  752.   } while (*++s);        /* loop until non-numeric character */
  753.   if (endp) *endp = s;        /* save users endp to after number */
  754.                 /* negate number if needed */
  755.   return (negative) ? -value : value;
  756. }
  757.